<?php

namespace App\Admin\Controllers;

use App\model\Project;
use App\Http\Controllers\Controller;
use App\model\TuiguangSource;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class ProjectController extends Controller
{
    use HasResourceActions;
 protected   $states = [
'on'  => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
'off' => ['value' => 0, 'text' => '关闭', 'color' => 'default'],
    ];
    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {

        return $content
            ->header('项目')
            ->description('')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {

        return $content
            ->header('编辑项目')
            ->description('')
            ->body($this->form(1)->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('创建项目')
            ->description('')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Project);
        $grid->model()->withoutGlobalScope('is_display');
        $grid->name('项目');
        $grid->baidu('百度提交链接')->display(function($value){

            return route('baiduFrom',['project_id'=>$this->project_id]);
        });
        $grid->toutiao('头条提交链接')->display(function($value){
            return route('toutiaoFrom',['project_id'=>$this->project_id]);
        });
        $grid->column('image','图片')->display(function($value){
            $image=\Storage::url($value);
            return "<img src='$image' width='100px' height='100px'>";
        });
        $grid->tengxun('腾讯提交链接')->display(function($value){
            return route('tengxunFrom',['project_id'=>$this->project_id]);
        });
        $grid->source('推广渠道')->pluck('name')->map(function ($name)use($grid) {
            return "<a  class='btn btn-sm btn-success'>$name</a>";
        })->implode(' ');

        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
        ];
        $grid->is_display('是否关闭')->switch($states);
        $grid->is_sea('是否加入公海管理')->switch($this->states);
        $grid->created_at('创建时间');
        $grid->actions(function(Grid\Displayers\Actions $actions){
            $actions->disableView();
        });
        $grid->disableFilter();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Project::withoutGlobalScope('is_display'));

        $show->project_id('Project id');
        $show->name('Name');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($edit='')
    {

     /*   if($edit==1) {
            $project=Project::withoutGlobalScope('is_display')->first();
        }else{
            $project=new Project();
        }*/

        $form = new Form(new Project());

        if ($edit=='') {
            $form->model()->withoutGlobalScope('is_display');
        }

        $form->text('name', '项目')->rules('required',[
            'required'=>'项目名称不能为空'
        ]);
        $form->multipleSelect('source','推广渠道')->options(TuiguangSource::getSelectOptions());
        $form->image('image','图片')->uniqueName();
        $states = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'primary'],
            'off' => ['value' => 2, 'text' => '关闭', 'color' => 'default'],
        ];
        $form->switch('is_display','是否关闭')->states($states);
        $form->switch('is_sea','是否加入公海管理')->states($this->states);

        return $form;

    }
    public function update($id)
    {

        return $this->form(1)->update($id);
    }
}
